// 替换变量值
export const replaceVarWithValues = (
  template: string,
  variables: Record<string, any>
): string => {
  let result = template
  
  Object.entries(variables).forEach(([key, value]) => {
    const regex = new RegExp(`{{${key}}}`, 'g')
    result = result.replace(regex, String(value || ''))
  })
  
  return result
}

// 将用户输入表单转换为提示变量
export const userInputsFormToPromptVariables = (
  inputs: Record<string, any>
): Record<string, any> => {
  const variables: Record<string, any> = {}
  
  Object.entries(inputs).forEach(([key, value]) => {
    variables[key] = value
  })
  
  return variables
}

// 验证必需的变量
export const validateRequiredVariables = (
  variables: Record<string, any>,
  requiredKeys: string[]
): { isValid: boolean; missingKeys: string[] } => {
  const missingKeys = requiredKeys.filter(key => 
    !variables[key] || variables[key].toString().trim() === ''
  )
  
  return {
    isValid: missingKeys.length === 0,
    missingKeys
  }
}

// 格式化提示模板
export const formatPromptTemplate = (
  template: string,
  variables: Record<string, any>
): string => {
  return replaceVarWithValues(template, variables)
} 